<?php

namespace app\admin\controller\training\statistics;

use app\admin\service\UserService;
use app\BaseController;
use app\model\DepartmentModel;
use app\model\training\TrainingTaskDepartmentModel;
use app\model\training\TrainingTaskModel;
use app\model\training\TrainingTaskUserModel;
use app\model\training\TrainingTypeModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function figure1(): bool|string
    {
        try {
            $TrainingTaskUserModel = new TrainingTaskUserModel();
            //累计报名
            $count_uid = $TrainingTaskUserModel->where('unit_id','=',$this->userData['unit_id'])->distinct(true)->column('uid');
            $data['count'] = count($count_uid);
            //通过
            $pass_uid = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['audit_status','=',2]
            ])->distinct(true)->column('uid');
            $data['pass_count'] = count($pass_uid);
            $UserModel = new UserModel();
            $data['boy'] = $UserModel->where([
                ['id','in',$pass_uid],
                ['sex','=',1]
            ])->count();
            $data['girl'] = $data['pass_count'] - $data['boy'];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function figure2(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'type|类型' => 'require|integer|in:1,2,3',
            ]);
            $time = match ($param['type']) {
                '1' => custom_time([], 'y'),
                '2' => custom_time([], 'm'),
                '3' => custom_time(),
                default => throw new \Exception('参数错误！'),
            };
            $data = [];
            $TrainingTaskUserModel = new TrainingTaskUserModel();
            foreach ($time as $item){
                $data['date'][] = $item['value'];
                $start_time = strtotime($item['start_time']);
                $end_time = strtotime($item['end_time']);
                $uid = $TrainingTaskUserModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['time','between',[$start_time,$end_time]],
                    ['audit_status','=',2]
                ])->column('uid');
                $uid2 = $TrainingTaskUserModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['time','between',[$start_time,$end_time]]
                ])->column('uid');
                $data['pass'][] = count(array_unique($uid));
                $data['count'][] = count(array_unique($uid2));
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function figure3(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $TrainingTaskDepartmentModel = new TrainingTaskDepartmentModel();
            $department = $TrainingTaskDepartmentModel->field('department_id,user_num')->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['user_num','>',0]
            ])->select()->toArray();
            $department_id = array_unique(array_column($department,'department_id'));
            $departmentData = [];
            foreach ($department as $v){
                $departmentData[$v['department_id']][] = $v['user_num'];
            }
            $DepartmentModel = new DepartmentModel();
            $data = $DepartmentModel->field('id')->where('id','in',$department_id)->paginate($param['pageSize'])->toArray();
            $UserService = new UserService($this->app);
            $allDelDepartment = $UserService->getNotDelDepartmentAffiliationAll($this->userData['unit_id']);
            foreach ($data['data'] as &$item){
                $item['user_num'] = array_sum($departmentData[$item['id']]??[]);
                $item['name'] = $allDelDepartment[$item['id']]['all_name']??'--';
            }
            $data['count'] = $TrainingTaskDepartmentModel->where('unit_id','=',$this->userData['unit_id'])->sum('user_num');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function figure4(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $TrainingTaskModel = new TrainingTaskModel();
            $count = $TrainingTaskModel->where('unit_id','=',$this->userData['unit_id'])->count();
            $data = $TrainingTaskModel->field('lecturer_id,count(id) as num')->where('unit_id','=',$this->userData['unit_id'])->with([
                'TrainingLecturer'
            ])->group('lecturer_id')->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item){
                $item['ratio'] = $item['num'] ? round(($item['num']/$count)*100,2) : 0;
            }
            return success([
                'count' => $count,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function figure5(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param, [
                'pageSize|每页显示数量' => 'require'
            ]);
            $TrainingTypeModel = new TrainingTypeModel();
            $TrainingTaskModel = new TrainingTaskModel();
            $TrainingTaskUserModel = new TrainingTaskUserModel();
            $uid = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['audit_status','=',2]
            ])->column('uid');
            $people = count(array_unique($uid));
            $data = $TrainingTypeModel->field('id,name')->where('unit_id','=',$this->userData['unit_id'])->paginate($param['pageSize'])->toArray();
            foreach ($data['data'] as &$item){
                $task_id = $TrainingTaskModel->where('type_id','=',$item['id'])->column('id');
                $uid = $TrainingTaskUserModel->where([
                    ['task_id','in',$task_id],
                    ['audit_status','=',2]
                ])->distinct(true)->column('uid');
                $item['people'] = count($uid);
            }
            return success([
                'people' => $people,
                'data' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    public function figure6(): bool|string
    {
        try {
            $TrainingTaskUserModel = new TrainingTaskUserModel();
            //心得反馈数
            $count = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_learning','=',1]
            ])->count();
            //听懂数
            $know_num  = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_know','=',1]
            ])->count();
            //听不懂数
            $not_know_num  = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_know','=',0]
            ])->count();
            //内容评分
            $content_level_count = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','>',0]
            ])->count();
            $content_level_1 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','=',1]
            ])->count();
            $content_level_2 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','=',2]
            ])->count();
            $content_level_3 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','=',3]
            ])->count();
            $content_level_4 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','=',4]
            ])->count();
            $content_level_5 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['content_level','=',5]
            ])->count();
            //讲师评价
            $lecturer_level_count = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','>',0]
            ])->count();
            $lecturer_level_1 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','=',1]
            ])->count();
            $lecturer_level_2 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','=',2]
            ])->count();
            $lecturer_level_3 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','=',3]
            ])->count();
            $lecturer_level_4 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','=',4]
            ])->count();
            $lecturer_level_5 = $TrainingTaskUserModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['lecturer_level','=',5]
            ])->count();
            return success([
                'lecturer_level_count' => $lecturer_level_count,
                'lecturer_level_1' => $lecturer_level_1,
                'lecturer_level_2' => $lecturer_level_2,
                'lecturer_level_3' => $lecturer_level_3,
                'lecturer_level_4' => $lecturer_level_4,
                'lecturer_level_5' => $lecturer_level_5,
                'content_level_count' => $content_level_count,
                'content_level_1' => $content_level_1,
                'content_level_2' => $content_level_2,
                'content_level_3' => $content_level_3,
                'content_level_4' => $content_level_4,
                'content_level_5' => $content_level_5,
                'count' => $count,
                'know_num' => $know_num,
                'not_know_num' => $not_know_num,
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}